package com.micro.config;

import com.micro.client.TrainServiceClient;
import com.micro.client.UserServiceClient;
import com.micro.request.MealOrderRequest;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@ConditionalOnProperty(name = "eureka.client.enabled", havingValue = "false", matchIfMissing = false)
public class MockConfig {

    @Bean
    @Primary
    public TrainServiceClient trainServiceClient() {
        return new MockTrainServiceClient();
    }

    @Bean
    @Primary
    public UserServiceClient userServiceClient() {
        return new MockUserServiceClient();
    }

    // 具体的Mock实现类
    public static class MockTrainServiceClient implements TrainServiceClient {
        @Override
        public Map<String, Object> getTrainByNumber(String trainNumber) {
            Map<String, Object> train = new HashMap<>();
            train.put("列车ID", 1L);
            train.put("车次", trainNumber);
            train.put("始发站ID", 1L);
            train.put("终点站ID", 2L);
            train.put("始发时间", "08:00:00");
            train.put("终到时间", "10:00:00");
            return train;
        }

        @Override
        public Map<String, Object> getTrainById(Long trainId) {
            Map<String, Object> train = new HashMap<>();
            train.put("列车ID", trainId);
            train.put("车次", "G1234");
            train.put("始发站ID", 1L);
            train.put("终点站ID", 2L);
            train.put("始发时间", "08:00:00");
            train.put("终到时间", "10:00:00");
            return train;
        }

        @Override
        public Long purchaseTicket(Long trainId, Integer seatType, Integer quantity, String travelDate) {
            return 1L; // 返回模拟的订单ID
        }

        @Override
        public Map<String, Object> getStationById(Long stationId) {
            Map<String, Object> station = new HashMap<>();
            station.put("站点ID", stationId);
            station.put("站点名称", "模拟站点");
            station.put("城市", "模拟城市");
            return station;
        }

        @Override
        public boolean processMealInventory(List<MealOrderRequest.MealOrderItemRequest> items) {
            return true; // 模拟餐品库存处理成功
        }
        
        @Override
        public Map<String, Object> getSeatById(Long seatId) {
            Map<String, Object> seat = new HashMap<>();
            seat.put("座位ID", seatId);
            seat.put("车厢ID", 1L);
            seat.put("座位号", "1A");
            seat.put("座位类型", "商务座");
            seat.put("状态", "已售");
            return seat;
        }
    }

    public static class MockUserServiceClient implements UserServiceClient {
        @Override
        public UserDTO getUserByUsername(String username) {
            UserDTO userDTO = new UserDTO();
            userDTO.setUserId(1L);
            userDTO.setUsername(username);
            userDTO.setEmail("mock@example.com");
            userDTO.setPhone("13800138000");
            userDTO.setBalance(new BigDecimal("1000.00"));
            return userDTO;
        }

        @Override
        public UserDTO getUserById(Long userId) {
            UserDTO userDTO = new UserDTO();
            userDTO.setUserId(userId);
            userDTO.setUsername("mockuser");
            userDTO.setEmail("mock@example.com");
            userDTO.setPhone("13800138000");
            userDTO.setBalance(new BigDecimal("1000.00"));
            return userDTO;
        }

        @Override
        public boolean consume(Long userId, BigDecimal amount) {
            return true; // 模拟消费成功
        }

        @Override
        public boolean recharge(String username, BigDecimal amount) {
            return true; // 模拟充值成功
        }
        
        @Override
        public PassengerDTO getPassengerByIdCard(String idCardNumber) {
            PassengerDTO passengerDTO = new PassengerDTO();
            passengerDTO.setPassengerId(1L);
            passengerDTO.setUserId(1L);
            passengerDTO.setPassengerName("模拟乘客");
            passengerDTO.setIdCardNumber(idCardNumber);
            passengerDTO.setPassengerType(0);
            passengerDTO.setGender(1);
            passengerDTO.setBirthDate("1990-01-01");
            passengerDTO.setPhoneNumber("13800138000");
            passengerDTO.setActive(true);
            return passengerDTO;
        }
    }
}
